package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.DriverTrailInfo;
import com.sgb.kjwl.model.entity.transport.SureStartTransportEntity;
import com.sgb.kjwl.model.entity.transport.VehicleDetail;
import com.sgb.kjwl.view.ui.activity.transport.SureStartTransportActivity;
import com.sgb.kjwl.view.ui.activity.vehicleManagement.VehicleDetailActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.mob.tools.utils.DeviceHelper.getApplication;

public class SureStartTransportModel extends AndroidViewModel {
    public SureStartTransportModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<Boolean> isSaved = new MutableLiveData<>();

    //上传多图片
    public void uploadMutiFileModel(SureStartTransportActivity activity, File file) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + "image.jpg\"", requestBody);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || TextUtils.isEmpty(response.body().getState())) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showLongToast(activity, "上传失败");
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().toString();
                    if (!TextUtils.isEmpty(url)) {
                        activity.uploadImgSuccess(url);
                    } else {
                        DialogHelper.dismissProgressDialog();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void sureStartTransport(SureStartTransportActivity activity, String orderNo, String images) {
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        map.put("images", images);
        APIManager.getInstance().getDriverAPI().sureStartTransport(map).enqueue(new Callback<BaseEntity<SureStartTransportEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SureStartTransportEntity>> call, Response<BaseEntity<SureStartTransportEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(activity, "网络出错");
                    return;
                }
                if (("200").equals(response.body().getData().getResult())) {
                    activity.postSureSuccessEvent();
                } else {
                    MToast.showToast(activity, response.body().getData().getMessage());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SureStartTransportEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void sureArrived(SureStartTransportActivity activity, String orderNo, String images) {
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        map.put("images", images);
        APIManager.getInstance().getDriverAPI().sureArrived(map).enqueue(new Callback<BaseEntity<SureStartTransportEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<SureStartTransportEntity>> call, Response<BaseEntity<SureStartTransportEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(activity, "网络出错");
                    return;
                }
                if (("200").equals(response.body().getData().getResult())) {
                    activity.postSureSuccessEvent();
                } else {
                    MToast.showToast(activity, response.body().getData().getMessage());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SureStartTransportEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void saveLocation(String orderNo) {
        DriverTrailInfo pushInfo = new DriverTrailInfo();
        pushInfo.setUserNo(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        pushInfo.setLatitude(Double.parseDouble(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE, "0")));
        pushInfo.setLongitude(Double.parseDouble(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE, "0")));
        pushInfo.setProvinceName(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.Province, ""));
        pushInfo.setCityName(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.City, ""));
        pushInfo.setDistrictName(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.Area, ""));
        pushInfo.setDetailAddress(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.Address, ""));
        pushInfo.setTime(System.currentTimeMillis());
        pushInfo.setOrderNo(orderNo);
        APIManager.getInstance().getDriverAPI().saveDriverTrail(pushInfo).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MLog.d("saveLocation", "轨迹上传成功");
                    isSaved.postValue(true);
                } else {
                    MLog.d("saveLocation", "轨迹上传失败");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {

            }
        });
    }

}
